home *** CD-ROM | disk | FTP | other *** search
/ Aminet 31 / Aminet 31 (1999)(Schatztruhe)[!][Jun 1999].iso / Aminet / util / libs / IdmLib.lha / IdmLib / Examples / GetID / GetID.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-28  |  3.5 KB  |  167 lines

  1. /*
  2. ** Created: Mon/22/Feb/1999
  3. **
  4. ** Example program that uses Idm.library to obtain a files identity.
  5. **
  6. ** Usage: GetID <nameoffile>
  7. **
  8. ** Compiled with DICE v3.x
  9. **
  10. ** This file is copyright © 1999 Andrew Bell.
  11. **
  12. */
  13.  
  14. #include <ainc:system.h>
  15. #include <GetID_rev.h>
  16. #include <proto/exec.h>
  17. #include <proto/dos.h>
  18. #include <proto/Idm.h>
  19. #include <libraries/Idm.h>
  20.  
  21. /*******************************************************************/
  22. /* Prototypes */
  23.  
  24. LONG main( void );
  25. BOOL InitPrg( void );
  26. void EndPrg( void );
  27. void DoErr( void );
  28. void DoPrg( void );
  29.  
  30. /*******************************************************************/
  31. /* Defines */
  32.  
  33. #define TITLE VERS " (" DATE ") Copyright © 1999 Andrew Bell.\n"
  34. #define ARGPLATE "FILE/A"
  35.  
  36. struct ArgLayout
  37. {
  38.     UBYTE *ARG_FILE;
  39. };
  40.  
  41. /*******************************************************************/
  42. /* Variables */
  43.  
  44. struct ArgLayout ArgData;
  45. struct RDArgs *ArgInfo    = NULL;
  46. struct Library *IdmBase   = NULL;
  47. struct IdmIdInfo *IdInfo  = NULL;
  48.  
  49. extern struct ExecBase *SysBase; /* let dlink pull these in */
  50. extern struct DOSBase *DOSBase;
  51.  
  52. /*******************************************************************/
  53.  
  54. LONG main( void )
  55. {
  56.     if (SysBase->LibNode.lib_Version < 37) return RETURN_FAIL;
  57.  
  58.     if (InitPrg())
  59.     {
  60.         VPrintf(TITLE, NULL);
  61.         DoPrg();
  62.     }
  63.     EndPrg();
  64.  
  65.     return RETURN_OK;
  66. }
  67.  
  68. static const UBYTE EVer[] = VERSTAG;
  69.  
  70. /*******************************************************************/
  71.  
  72. BOOL InitPrg( void )
  73. {
  74.     if (!(ArgInfo = ReadArgs(ARGPLATE, &ArgData, NULL)))
  75.     {
  76.         DoErr(); return FALSE;
  77.     }
  78.  
  79.     if (!(IdmBase = OpenLibrary(IDMNAME, IDMLIBVERSION)))
  80.     {
  81.         VPrintf("Sorry, you really need " IDMNAME " to use this program!\n", NULL);
  82.         return FALSE;
  83.     }
  84.  
  85.     if (!(IdInfo = IdmAllocIdInfo(NULL)))
  86.     {
  87.         VPrintf("Unable to obtain init Idm.library!\n", NULL);
  88.         return FALSE;
  89.     }
  90.  
  91.     return TRUE;
  92. }
  93.  
  94. /*******************************************************************/
  95.  
  96. void EndPrg( void )
  97. {
  98.     if (IdInfo) { IdmFreeIdInfo(IdInfo); IdInfo = NULL; }
  99.     if (IdmBase) { CloseLibrary(IdmBase); IdmBase = NULL; }
  100.     if (ArgInfo) FreeArgs(ArgInfo);
  101. }
  102.  
  103. /*******************************************************************/
  104.  
  105. void DoErr( void )
  106. {
  107.     PrintFault(IoErr(), "GetID error ");
  108. }
  109.  
  110. /*******************************************************************/
  111.  
  112. void DoPrg( void )
  113. {
  114.     /*
  115.     ** REMEMBER: If you do not load the whole file to memory then you MUST
  116.     **           use the following function to determine the smallest amount
  117.     **           of bytes required to be loaded off the start of the file.
  118.     */
  119.  
  120.     ULONG ReadLen = IdmObtainIOLen();
  121.  
  122.     BPTR InFile = Open(ArgData.ARG_FILE, MODE_OLDFILE);
  123.  
  124.     if (InFile)
  125.     {
  126.         APTR FileBit = AllocVec(ReadLen, MEMF_CLEAR);
  127.  
  128.         if (FileBit)
  129.         {
  130.             ULONG r = Read(InFile, FileBit, ReadLen);
  131.  
  132.             if (r != ~NULL && r != NULL)
  133.             {
  134.                 /*
  135.                 ** Below is the function that does the identification. The following
  136.                 ** paramters are passed:
  137.                 **
  138.                 ** IdInfo   - This pointer is return by IdmAllocIdInfo()
  139.                 ** FileBit  - Pointer to file is memory.
  140.                 ** r        - Length of file (or the amount of bytes loaded off it)
  141.                 ** NULL     - Taglist, non are defined so pass a NULL for now.
  142.                 */
  143.  
  144.                 UBYTE *FileIDString = IdmIdentifyMem(IdInfo, FileBit, r, NULL);
  145.  
  146.                 if (FileIDString)
  147.                 {
  148.                     /* We get a pointer if the filetype is known */
  149.  
  150.                     VPrintf("This file is a: %s\n", &FileIDString);
  151.                 }
  152.                 else
  153.                 {
  154.                     /* Else we get a NULL if the filetype is unknown */
  155.  
  156.                     VPrintf("Idm.library was unable to identify this file!\n", NULL);
  157.                 }
  158.             }
  159.             else DoErr();
  160.  
  161.             FreeVec(FileBit);
  162.         }
  163.         Close(InFile);
  164.     }
  165.     else DoErr();
  166. }
  167.